<!DOCTYPE html>
<html lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html" charset="UTF-8">
        <title>WebSocket客户端</title>
        <script type="text/javascript">

            function checkCookie() {
                alert('1')
            }

            function link() {
                var socket;
                if(!window.WebSocket){
                    window.WebSocket = window.MozWebSocket;
                }
                if(window.WebSocket){
                    socket = new WebSocket("ws://localhost:8888/websocket?userId=1");
                    socket.onmessage = function (ev) {
                        var ta = document.getElementById("responseContent");
                        ta.value += ev.data +"\r\n";
                    };
                    socket.onopen = function (ev) {
                        var ta = document.getElementById("responseContent");
                        ta.value += "请继续操作" +"\r\n";
                    };
                    socket.onclose = function (ev) {
                        var ta = document.getElementById("responseContent");
                        ta.value = "websocket 连接关闭";
                    };

                }else{
                    alert("浏览器不支持websocket")
                }
            }

            function send(message) {
                if(!window.WebSocket){
                    return;
                }
                if(socket.readyState == WebSocket.OPEN){
                    socket.send(message)
                }else{
                    alert("websocket 连接未成功")
                }
            }
        </script>
    </head>
    <body>
        <form onsubmit="return false;">
            <input type="text" name="message" value="">
            <br><br>
            <input type="button" onclick="send(this.form.message.value)" value="发送">

            <hr color="red">
            <h2>客户端接收服务端应答消息</h2>
            <textarea id="responseContent" style="width: 1024px;height: 300px;"></textarea>
        </form>
    </body>
</html>